﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Silvermoon.Navigation;
using Silvermoon.UI;
using Silvermoon.Controls.Base;
using Silvermoon.Controls;
using Silvermoon.Animations;
using Silvermoon;
using OutlookHub.Pages;

namespace OutlookHub.Utils
{
    public static class PageTransitions
    {

        /// <summary>
        /// Defines transitions between pages.
        /// </summary>
        public static void DefineTransitions()
        {
            NavigationService.SetTransition<Page, TaskDetailsPage>(PageTransitions.InOut);
            NavigationService.SetTransition<TaskDetailsPage, TasksPage>(PageTransitions.OutIn);


            NavigationService.SetTransition<Page, EditTaskDetailsPage>(PageTransitions.InOut);
            NavigationService.SetTransition<EditTaskDetailsPage, TasksPage>(PageTransitions.OutIn);
            NavigationService.SetTransition<TaskDetailsPage, EditTaskDetailsPage>(PageTransitions.Flip);

            NavigationService.SetTransition(typeof(Page), typeof(TaskSortOrderPage), PageTransitions.FadeIn);
            NavigationService.SetTransition(typeof(TaskSortOrderPage), typeof(TasksPage), PageTransitions.FadeOut);

            NavigationService.SetTransition(typeof(TasksPage), typeof(CategoriesPage), PageTransitions.FromBottom);
            NavigationService.SetTransition(typeof(CategoriesPage), typeof(TasksPage), PageTransitions.ToBottom);

            NavigationService.SetTransition(typeof(EditTaskDetailsPage), typeof(CategoriesPage), PageTransitions.InOut);
            NavigationService.SetTransition(typeof(CategoriesPage), typeof(EditTaskDetailsPage), PageTransitions.OutIn);

            NavigationService.SetTransition<ContactPage, ContactDetailsPage>(PageTransitions.ZoomIn);
            NavigationService.SetTransition<ContactDetailsPage, ContactPage>(PageTransitions.OutIn);

            NavigationService.SetTransition(typeof(Page), typeof(EMailDetailsPage), PageTransitions.OutIn);
            NavigationService.SetTransition(typeof(EMailDetailsPage), typeof(EMailsPage), PageTransitions.InOut);

            NavigationService.SetTransition(typeof(Page), typeof(EMailFoldersPage), PageTransitions.FromBottom);
            NavigationService.SetTransition(typeof(EMailFoldersPage), typeof(EMailsPage), PageTransitions.ToBottom);


        }

        const int duration = 110;
        private static readonly ScaleTransform explodeTransform = new ScaleTransform(5f, 5f, 1f);
        private static readonly ScaleTransform minimizedTransform = new ScaleTransform(0.8f, 0.8f, 1f);


        public static readonly PageTransition FadeIn = new PageTransition
        {
            Mode = PageTransitionMode.Show,
            Target = new VisualState("", new IntTransition(FormBase.OpacityProperty, 350, 0, 255, LinearEase.Instance))
        };

        public static readonly PageTransition FadeOut = new PageTransition
        {
            Mode = PageTransitionMode.Hide,
            Source = new VisualState("", new IntTransition(FormBase.OpacityProperty, 400, null, 0, LinearEase.Instance))
        };

        public static readonly PageTransition ShiftLeft = new PageTransition
        {
            Mode = PageTransitionMode.HideAndShow,
            Source = new VisualState("", new FloatTransition(FormBase.TranslateXFactorProperty, 350, null, -1f, PowerEase.Out)),
            Target = new VisualState("", new FloatTransition(FormBase.TranslateXFactorProperty, 350, 1f, 0f, PowerEase.Out)),
        };

        public static readonly PageTransition ShiftRightTransition = new PageTransition
        {
            Mode = PageTransitionMode.HideAndShow,
            Source = new VisualState("", new FloatTransition(FormBase.TranslateXFactorProperty, 350, null, 1f, PowerEase.Out)),
            Target = new VisualState("", new FloatTransition(FormBase.TranslateXFactorProperty, 350, -1f, 0f, PowerEase.Out)),
        };

        public static readonly PageTransition Implode = new PageTransition
        {
            Mode = PageTransitionMode.Show,
            Target = new VisualState("",
                new IntTransition(Control.OpacityProperty, 350, 0, 255, PowerEase.In),
                new ScaleTransition(350, explodeTransform, ScaleTransform.One, PowerEase.QuadraticOut))
        };

        public static readonly PageTransition Explode = new PageTransition
        {
            Mode = PageTransitionMode.Hide,
            Source = new VisualState("",
                new IntTransition(Control.OpacityProperty, 350, null, 0, LinearEase.Instance),
                new ScaleTransition(350, null, explodeTransform, PowerEase.In))

        };

        public static readonly PageTransition ZoomIn = new PageTransition
        {
            Mode = PageTransitionMode.Show,
            Target = new VisualState("",
                new IntTransition(Control.OpacityProperty, 200, 0, 255, PowerEase.Out),
                new ScaleTransition(350, ScaleTransform.Zero2D, ScaleTransform.One, PowerEase.QuadraticOut))
        };

        public static readonly PageTransition ZoomOut = new PageTransition
        {
            Mode = PageTransitionMode.Hide,
            Source = new VisualState("",
                new IntTransition(Control.OpacityProperty, 350, null, 0, PowerEase.CubicIn),
                new ScaleTransition(350, null, explodeTransform, PowerEase.In))
        };


        public static readonly PageTransition FromBottom = new PageTransition
        {
            Mode = PageTransitionMode.Show,
            Target = new VisualState("",
                new IntTransition(Control.OpacityProperty, 300, 0, 255),
                new FloatTransition(FormBase.TranslateYFactorProperty, 350, 1f, 0f, PowerEase.Out))
        };

        public static readonly PageTransition ToBottom = new PageTransition
        {
            Mode = PageTransitionMode.Hide,
            Source = new VisualState("",
                new IntTransition(Control.OpacityProperty, 300, null, 80, LinearEase.Instance) { Delay = 100 },
                new FloatTransition(FormBase.TranslateYFactorProperty, 300, null, 1f, PowerEase.In) { Delay = 50 })
        };

        public static readonly PageTransition RotateLeftIn = new PageTransition
        {
            Mode = PageTransitionMode.HideThenShow,
            Source = new VisualState("", new FloatTransition(FormBase.RotateVerticalProperty, 350, 2f * -360F, 2f * -360F + 90F, CircleEase.Out)),
            Target = new VisualState("", new FloatTransition(FormBase.RotateVerticalProperty, 350, -360f * 2f + 90F, 2f * -360F, CircleEase.In))
        };

        public static readonly PageTransition RotateLeftOut = new PageTransition
        {
            Mode = PageTransitionMode.HideThenShow,
            Source = new VisualState("", new FloatTransition(FormBase.RotateVerticalProperty, 350, -360f * 2f, 2f * -360F - 90F, CircleEase.Out)),
            Target = new VisualState("", new FloatTransition(FormBase.RotateVerticalProperty, 350, -360f * 2f + 90F, 2f * -360F, CircleEase.In))
        };

        public static readonly PageTransition Flip = new PageTransition
        {
            Mode = PageTransitionMode.HideThenShow,
            Source = new VisualState("", new FloatTransition { Property = FormBase.RotateHorizonProperty, To = 90F, EasingFunction = CircleEase.In, Duration = duration }),

            Target = new VisualState(VState.Shown,
                new FloatTransition { Property = FormBase.VisibilityProperty, From = -0f, To = 1F, EasingFunction = LinearEase.Instance, Duration = duration },
                new IntTransition(FormBase.OpacityProperty, 1, null, 255),
                new FloatTransition { Property = FormBase.RotateHorizonProperty, From = -90F, To = 0F, EasingFunction = BackEase.Out, Duration = duration * 4 }),

        };

        public static readonly PageTransition InOut = new PageTransition
        {
            Mode = PageTransitionMode.HideThenShow,
            Source = new VisualState("",
//                new IntTransition(Control.OpacityProperty, 150, null, 0, PowerEase.QuadraticIn),
                new ScaleTransition(100, null, minimizedTransform, PowerEase.QuadraticIn)
                ),

            Target = new VisualState("",
            new IntTransition(Control.OpacityProperty, 200, 0, 255, LinearEase.Instance),
            new ScaleTransition(250, explodeTransform, ScaleTransform.One, PowerEase.Out))
        };

        public static PageTransition OutIn = new PageTransition
        {
            Mode = PageTransitionMode.HideThenShow,
            Source = new VisualState("",
                new IntTransition(Control.OpacityProperty, 200, null, 0, PowerEase.QuadraticIn),
                new ScaleTransition(250, null, explodeTransform, PowerEase.QuadraticIn)),

            Target = new VisualState("",
//            new IntTransition(Control.OpacityProperty, 100, 48, 255, LinearEase.Instance),
            new ScaleTransition(100, minimizedTransform, ScaleTransform.One, PowerEase.QuadraticOut))
        };
    }
}
